package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"nm/models"
)

type Shop struct {
	beego.Controller
}

func (c *Shop) Add() {
	name := c.GetString("name")
	price, _ := c.GetFloat("price")
	img := c.GetString("img")
	if name == "" || img == "" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "参数为空",
			Data: nil,
		}
		c.ServeJSON()
	}
	var shop = models.Shop{
		Name:  name,
		Price: price,
		Img:   img,
	}
	models.DB.Create(&shop)
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "添加成功",
		Data: nil,
	}
	c.ServeJSON()
}
func (c *Shop) Del() {
	id, _ := c.GetInt("id")
	var user []models.Shop
	models.DB.Where("id =?", id).Delete(&user)
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "删除成功",
		Data: nil,
	}
	c.ServeJSON()

}
func (c *Shop) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var msensj []models.Shop
	models.DB.Find(&msensj)
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: msensj,
	}
	c.ServeJSON()
}
func (c *Shop) Update() {
	id, _ := c.GetInt("id")
	name := c.GetString("name")
	price, _ := c.GetFloat("price")
	img := c.GetString("img")
	var use = models.Shop{
		Name:  name,
		Price: price,
		Img:   img,
	}
	models.DB.Where("id =?", id).Updates(&use)
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "修改成功",
		Data: nil,
	}
	c.ServeJSON()
}
